# -*-coding:utf-8 -*-
# 定时任务:判断心跳时间,从而判断是否需要关闭这个uri
from company.models import VehicleCompanyTerminal
from media.models import VideoStream
from datetime import datetime, timedelta

from media.player import Player


def close_video_uri():
    # 每三分钟遍历一次视频,判断是否有超出三分钟的
    video_stream = VideoStream.objects.all()
    for stream in video_stream:
        if stream.modified_on + timedelta(minutes=3) < datetime.now():
            player = Player()
            # 发起关闭直播请求(需要判断是开易还是小卓)
            vct = VehicleCompanyTerminal.objects.get(terminal=stream.terminal)
            vehicle_id = vct.vehicle_id
            # 发送关闭指令
            player.stop(vehicle_id, stream.channel_id, vct.terminal.device_type, vct.terminal, vct)
            # 删除直播
            stream.delete()


if __name__ == '__main__':
    close_video_uri()
